package com.niodata.dp.monitor.sharelib.entity.base;

import com.niodata.dp.monitor.common.util.ThrowableUtil;
import java.util.List;

/**
 * Created by zhangtianyu on 2017/7/5.
 */
public class BaseResult<T> {

  private Long timestamp;
  private Boolean success;
  private String errorMsg;
  private String errorDetail;
  private List<T> data;

  public Long getTimestamp() {
    return timestamp;
  }

  public void setTimestamp(Long timestamp) {
    this.timestamp = timestamp;
  }

  public Boolean getSuccess() {
    return success;
  }

  public void setSuccess(Boolean success) {
    this.success = success;
  }

  public String getErrorMsg() {
    return errorMsg;
  }

  public void setErrorMsg(String errorMsg) {
    this.errorMsg = errorMsg;
  }

  public String getErrorDetail() {
    return errorDetail;
  }

  public void setErrorDetail(String errorDetail) {
    this.errorDetail = errorDetail;
  }

  public List<T> getData() {
    return data;
  }

  public void setData(List<T> data) {
    this.data = data;
  }

  public BaseResult<T> buildErrorResult(Throwable t) {
    this.setSuccess(false);
    this.setErrorMsg(t.getMessage());
    this.setErrorDetail(ThrowableUtil.getErrorDetailFromThrowable(t));
    this.setTimestamp(System.currentTimeMillis());
    return this;
  }

  public BaseResult<T> buildSuccessResult(List<T> dataList, boolean success) {
    this.setSuccess(success);
    if (dataList != null) {
      this.setData(dataList);
    }
    this.setTimestamp(System.currentTimeMillis());
    return this;
  }

  public BaseResult<T> buildSuccessResult(boolean success) {
    return this.buildSuccessResult(null, success);
  }
}
